package com.iterator.service.web.marketManager;

import com.iterator.bean.web.lc.CategoryBean.CategoryCreateBO;
import com.iterator.bean.web.lc.CategoryBean.CategoryCreateVO;
import com.iterator.bean.web.lc.CategoryBean.CategoryListVO;
import com.iterator.bean.web.lc.CategoryBean.CategoryUpdateBO;
import com.iterator.bean.web.lc.CategoryBean.ChildrenBean;
import com.iterator.bean.web.pojo.Category;
import com.iterator.bean.web.pojo.CategoryExample;
import com.iterator.mapper.web.marketManager.CategoryMapperByLichao;
import com.iterator.mapper.web.marketManager.CustomCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapperByLichao categoryMapperByLichao;
    @Autowired
    CustomCategoryMapper customCategoryMapper;

    @Override
    public List<CategoryListVO> getList() {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        //exclude deleted element
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapperByLichao.selectByExample(example);
        List<CategoryListVO> categoryListVOS = new ArrayList<>();
        //遍历全部category获得一级目录
        Iterator<Category> iterator = categories.iterator();
        //此处必须用Iterator，不然会报并发修改异常
        while (iterator.hasNext()){
            Category category= iterator.next();
            if (category.getLevel().equals("L1")) {
                categoryListVOS.add(new CategoryListVO(category.getId(),
                        category.getName(),
                        category.getKeywords(),
                        category.getDesc(),
                        category.getIconUrl(),
                        category.getPicUrl(),
                        category.getLevel(),
                        new ArrayList<>()));
                iterator.remove();
            }
        }
        //填充二级目录
        Iterator<Category> iterator1 = categories.iterator();
        while (iterator1.hasNext()){
            Category category = iterator1.next();
            if(category.getLevel().equals("L2")){
                for (CategoryListVO categoryListVO : categoryListVOS) {
                    if(category.getPid() == categoryListVO.getId()){
                        ChildrenBean childrenBean = new ChildrenBean(category.getId(),
                                category.getName(),
                                category.getKeywords(),
                                category.getDesc(),
                                category.getIconUrl(),
                                category.getPicUrl(),
                                category.getLevel());
                        categoryListVO.addChild(childrenBean);
                        iterator1.remove();
                    }
                }
            }
        }
        for (Category category : categories) {
            categoryListVOS.add(new CategoryListVO(
                    category.getId(),
                    category.getName(),
                    category.getKeywords(),
                    category.getDesc(),
                    category.getIconUrl(),
                    category.getPicUrl(),
                    category.getLevel(),
                    null));
        }
        return categoryListVOS;
    }

        @Override
        public List<CategoryListVO> getL1() {
            CategoryExample example = new CategoryExample();
            CategoryExample.Criteria criteria = example.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<Category> categories = categoryMapperByLichao.selectByExample(example);
            List<CategoryListVO> categoryListVOS = new ArrayList<>();
            //遍历全部category获得一级目录
            Iterator<Category> iterator = categories.iterator();
            while (iterator.hasNext()){
                Category category= iterator.next();
                if (category.getLevel().equals("L1")) {
                    categoryListVOS.add(new CategoryListVO(category.getId(),
                            category.getName(),
                            null,
                            null,
                            null,
                            null,
                            null,
                            null));
                    iterator.remove();
                }
            }
            return categoryListVOS;
        }

    @Override
    public CategoryCreateVO create(CategoryCreateBO createBO) {
        Date date = new Date();
        Category category = new Category(null,
                createBO.getName(),createBO.getKeywords(),createBO.getDesc(),createBO.getPid(),createBO.getIconUrl()
        ,createBO.getPicUrl(),createBO.getLevel(),null,date,date,false);
        int id = categoryMapperByLichao.insert(category);
        return new CategoryCreateVO(id,category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getAddTime(),category.getUpdateTime());
    }

    @Override
    public Integer update(CategoryUpdateBO updateBO) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(updateBO.getId());
        return categoryMapperByLichao.updateByExampleSelective(new Category(updateBO.getId(),
                updateBO.getName(),
                updateBO.getKeywords(),
                updateBO.getDesc(),
                updateBO.getPid(),
                updateBO.getIconUrl(),
                updateBO.getPicUrl(),
                updateBO.getLevel(),
                null, null, new Date(), false), example);
    }

    @Override
    public Integer delete(CategoryUpdateBO updateBO) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(updateBO.getId());
        Category category = new Category();
        category.setDeleted(true);
        return categoryMapperByLichao.updateByExampleSelective(category,example);
    }
}




